import 'package:flutter/material.dart';
import 'package:aio/aio.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/manager/audit_manager.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_attachment.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_contractInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_customersInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_new_customer.dart';
import 'package:app/src/ui/route/works/public/items/ui_node_list.dart';
import 'package:app/src/ui/route/works/public/items/ui_note.dart';
import 'package:app/src/ui/route/works/public/items/ui_payment.dart';
import 'package:app/src/ui/route/works/public/acom/ui_product.dart';
import 'package:app/src/ui/route/works/public/items/ui_specialclauses.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';
import 'package:app/src/ui/route/works/public/review_submitted.dart';

import '../../../../http/bean/works_2023/contractroyaltyrateapplycheck_bean.dart';
import '../../../../http/model/review_model.dart';
import '../../../dialog/dailog.dart';
import '../../helper/route_helper.dart';
import '../public/acom/ui_addresslis.dart';
import '../public/acom/ui_contractFreightInfo.dart';

///合同审核详情；（提成比例审核跳转过来的合同信息）
class ContractDetailsRoute2 extends StatefulWidget {
  String ContractCode; //合同编号
  String ContractId; //合同编号ID
  bool onlyBack = false; //只有返回键；
  ContractRoyaltyRateApplyCheckBean? ContractRoyaltyRateApplyCheck; //提成比例审核数据

  ContractDetailsRoute2(this.ContractCode, this.ContractId, this.onlyBack,
      this.ContractRoyaltyRateApplyCheck);

  @override
  _State createState() => _State();
}

class _State extends State<ContractDetailsRoute2> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    presenter.getContractapplygetinfoWithDelay2(
        context, widget.ContractCode, widget.ContractId); //获取审核详情数据
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body);
  }

  ///产品搜索条件实体类
  SearchProduct searchProduct = SearchProduct();

  Widget _body(BuildContext context) {
    if (presenter?.mDatas == null) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    // children.add(UiNewCustomer.build(
    //     context, presenter?.mDatas?.ContractInfo)); //此客户是新客户;(提成比例详情里，不用显示这个。)
    children.add(UiCustomersInfo.build(
        context,
        presenter?.mDatas?.CustomersInfo,
        presenter?.mOldDatas?.CustomersInfo)); //客户信息
    children.add(UiContractInfo.build(context, presenter?.mDatas?.ContractInfo,
        presenter?.mOldDatas?.ContractInfo,
        ContractRoyaltyRateApplyCheck:
            widget.ContractRoyaltyRateApplyCheck)); //基本信息（合同信息）
    children.add(UiAddresslis.build(context, presenter?.mDatas?.getAddresslis(),
        presenter?.mOldDatas?.getAddresslis())); //送货地址
    children.add(UiContractFreightInfoList.build(
        context,
        presenter?.mDatas?.ContractFreightInfoList,
        presenter?.mOldDatas?.ContractFreightInfoList)); //送货路线
    children.add(UiPayment.build(
        context, presenter?.mDatas?.ContractPaymentTermsCopy)); //付款方式
    children.add(UiPayment.buildWithLast(
        context, presenter?.mDatas?.ContractFinalPaymentCopy)); //末批发货
    children.add(UiSpecialClauses.build(
        context, presenter?.mDatas?.SpecialClausesList)); //特殊加工
    children.add(UiAttachment.build(context, presenter?.mDatas?.FileList)); //附件
    children.add(UiNote.build(
        context,
        presenter?.mDatas?.ContractInfo?.ContractContent,
        presenter?.mOldDatas?.ContractInfo?.ContractContent,
        title: AppStrings.of(context).ContractNote)); //合同备注
    children.add(UiProduct.build(
        context,
        searchProduct,
        presenter?.mDatas?.ProductList,
        presenter?.mOldDatas?.ProductList,true)); //产品信息
    children.add(UiNoteList.build(context, presenter?.mDatas?.NodeList)); //审核日志
    return Column(
      children: [
        Expanded(child: UiBase.listView(context, children)),
        Visibility(
            child: ReviewSubmittedStateful(
              presenter?.mDatas,
              onlyBack: widget.onlyBack,
            ),
            visible: presenter?.mDatas != null)
      ],
    );
  }
}

class _Presenter extends ReviewPresenter {
  ///获取审核详情数据
  getContractapplygetinfo2(
      BuildContext context, String ContractCode, String ContractId,
      {ResponseCallbackWithVoid? callback = null}) {
    isLoad = true;
    //获取详情数据
    ReviewModel.getContractapplygetinfo2(
        context, ContractCode, ContractId, true, History: false,
        onError: (code, error) {
      isLoad = false;
      setStateBody?.call(() {});
      AppDialog.showDialogWithError(context, code, error)?.then((value) {
        AppRouteHelper.pop(context); //网络异常，关闭当前页面
      });
    })?.then((value) {
      isLoad = false;
      if (value.isSuccessForCode()) {
        try {
          mDatas = value;
          setStateBody?.call(() {});
          callback?.call();
        } catch (e) {}
      } else {
        //异常
        AppDialog.showDialogWithInfo(context, content: value.MsgDetail)
            ?.then((value) {
          AppRouteHelper.pop(context); //关闭当前页面
        });
      }
    });
  }

  ///获取审核详情数据[延迟获取，等待页面加载完成]
  getContractapplygetinfoWithDelay2(
      BuildContext context, String ContractCode, String ContractId,
      {ResponseCallbackWithVoid? callback = null}) {
    Future.delayed(Duration(milliseconds: 200)).then((e) {
      getContractapplygetinfo2(context, ContractCode, ContractId,
          callback: callback); //获取审核详情数据
    });
  }
}
